package com.crm.system.service.impl.customerManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.customerManager.CustomerPoolCompany;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.service.customerManager.CustomerPoolCompanyDbService;
import com.crm.system.service.customerManager.CustomerPoolCompanyService;
import com.crm.system.service.permissionManagement.SysCompanyService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/1/17 15:04
 */
@Service
public class CustomerPoolCompanyServiceImpl implements CustomerPoolCompanyService {
    @Autowired
    CustomerPoolCompanyDbService customerPoolCompanyDbService;

    @Autowired
    SysCompanyService sysCompanyService;

    @Override
    public Boolean addCustomerPoolCompany(List<String> companyIdList, String customerPoolId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        companyIdList.stream().forEach(companyId ->{
            CustomerPoolCompany customerPoolCompany = new CustomerPoolCompany();
            customerPoolCompany.setCustomerPollCompanyId(CommonUtils.getGUID());
            customerPoolCompany.setCustomerPoolId(customerPoolId);
            customerPoolCompany.setCompanyId(companyId);
            customerPoolCompany.setCreateTm(new Date());
            customerPoolCompany.setCreatePeopleId(userAccount.getAccountId());
            customerPoolCompanyDbService.insertCustomerPoolCompany(customerPoolCompany);
        });
        return true;
    }

    @Override
    public List<JSONObject> selectCustomerPoolCompany(String customerPoolId) {
        List<JSONObject> companyVOs = new ArrayList<>();
        List<SysCompany> sysCompanyList = sysCompanyService.selectSysCompanyList();
        List<CustomerPoolCompany> customerPoolCompanyList = customerPoolCompanyDbService.selectCompanyByCustomerPoolId(customerPoolId);
        sysCompanyList.stream().forEach(company -> {
            JSONObject companyVO = new JSONObject();
            companyVO.put("companyId",company.getCompanyId());
            companyVO.put("companyName",company.getCompanyName());
            //判断管理用户id集合里面如果包含账号id设置为true
            Boolean result = false;
            for (CustomerPoolCompany customerPoolCompany : customerPoolCompanyList) {
                if(customerPoolCompany.getCompanyId().equals(company.getCompanyId())){
                    result = true;
                }

            }
            companyVO.put("selected",result);
            companyVOs.add(companyVO);
        });
        return companyVOs;
    }

    @Override
    public Boolean deleteCustomerPoolCompanyByCluePoolId(String customerPoolId) {
        return customerPoolCompanyDbService.deleteCustomerPoolCompanyByCluePoolId(customerPoolId);
    }
}
